#ifndef EVENTRECEIVER_H
#define EVENTRECEIVER_H
#define UNICODE
#define _UNICODE

#include "GuiID.h"
#include "NodeID.h"
#include<fstream>
#include<string>
#include<iostream>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
using namespace std;

//core::stringw ipstr;

struct SAppContext
{
	IrrlichtDevice *device;
	s32				counter;
	IGUIListBox*	listbox;
};

class EventReceiver : public IEventReceiver
{
public:
	struct SMouseState
	{
		core::position2di delta;
		core::position2di Position;
		bool LeftButtonDown;
		bool RightButtonDown;
		int deltaWheel;
		SMouseState() : LeftButtonDown(false),RightButtonDown(false),deltaWheel(0){ }
	} MouseState;


	EventReceiver(SAppContext & context): Context(context),PlayerEndGame(false),LevelOne(false),LevelTwo(false)
		,PlayerSaveGame(false),StartUnfinishedGame(false),StartNewGame(false),PlayerNotSaveGame(false),
		SendMessage(false),PlayOnLineGame(false),BlackSide(false),undotimes(0),PlayerUndo(false),
		PlayerSetWeb(false),PlayerCannotUndo(false),PlayerQuit(false),PlayerCanQuit(false),PlayBlackSide(false),
		PlayWhiteSide(false)
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
		ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	}

	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
			return false;
		}
		if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
		{
			switch(event.MouseInput.Event)
			{
			case EMIE_LMOUSE_PRESSED_DOWN:
				MouseState.LeftButtonDown = true;
				MouseState.delta.X=0;
				MouseState.delta.Y=0;
				break;

			case EMIE_RMOUSE_PRESSED_DOWN:
				MouseState.RightButtonDown=true;
				MouseState.delta.X=0;
				MouseState.delta.Y=0;
				break;

			case EMIE_LMOUSE_LEFT_UP:
				MouseState.LeftButtonDown = false;
				MouseState.delta.X=0;
				MouseState.delta.Y=0;
				break;

			case EMIE_RMOUSE_LEFT_UP:
				MouseState.RightButtonDown = false;
				MouseState.delta.X=0;
				MouseState.delta.Y=0;
				break;


			case EMIE_MOUSE_MOVED:
				MouseState.delta.X=event.MouseInput.X-MouseState.Position.X;
				MouseState.delta.Y=event.MouseInput.Y-MouseState.Position.Y;

				MouseState.Position.X = event.MouseInput.X;
				MouseState.Position.Y = event.MouseInput.Y;
				break;

			case EMIE_MOUSE_WHEEL:
				MouseState.deltaWheel=event.MouseInput.Wheel;
				break;

			default:
				// We won't use the wheel
				break;
			}
		}

		/*if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
			return false;
		}*/
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = Context.device->getGUIEnvironment();

			switch(event.GUIEvent.EventType)
			{
			case EGET_MENU_ITEM_SELECTED:
				OnMenuItemSelected((IGUIContextMenu*)event.GUIEvent.Caller);
				break;

			case EGET_SCROLL_BAR_CHANGED:
				if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

					for (u32 i=0; i<EGDC_COUNT ; ++i)
					{
						SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
						col.setAlpha(pos);
						env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
					}
				}
				if(id == GUI_ID_COLOR_SCROLL_BAR_RED)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

					SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)0);
					col.setRed(pos);
					env->getSkin()->setColor((EGUI_DEFAULT_COLOR)0,col);
				}
				if(id == GUI_ID_COLOR_SCROLL_BAR_GREEN)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

					SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)0);
					col.setGreen(pos);
					env->getSkin()->setColor((EGUI_DEFAULT_COLOR)0,col);
				}
				if(id == GUI_ID_COLOR_SCROLL_BAR_BLUE)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

					SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)0);
					col.setBlue(pos);
					env->getSkin()->setColor((EGUI_DEFAULT_COLOR)0,col);
				}
				if(id == GUI_ID_BACKGROUND_COLOR_BAR)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

					SColor col;
				}
				break;


			case EGET_BUTTON_CLICKED:
				switch(id)
				{
				case GUI_ID_BUTTON_PLAY_BLACKSIDE:
					PlayBlackSide = true;
					break;
				case GUI_ID_BUTTON_PLAY_WHITESIDE:
					PlayWhiteSide = true;
					PlayBlackSide = false;
					break;
				case GUI_ID_BUTTON_UNDO:
					if(LevelOne || LevelTwo)
					SetPlayerUndo(true);
					break;
				case GUI_ID_BUTTON_ONLINE_GAME_SIDE_BLACK:
					Context.device->getGUIEnvironment()->addMessageBox(L"warning",
						L"Player chooses blackside!",false);
					BlackSide = true;
					break;
				case GUI_ID_BUTTON_CLOSE_CONTROL_HELP:
					closeControlHelp();
					break;
				case GUI_ID_BUTTON_END_GAME:
					StartNewGame = false;
					PlayerEndGame = true;
					SaveGameSubWindow();
					break;
				case GUI_ID_BUTTON_GAME_LEVEL_ONE:
					/*Context.device->getGUIEnvironment()->addMessageBox(L"warning",
						L"Start Game Level One!",false);*/
					selectSideSubWindow();
					chooseLevelOne();
					break;
				case GUI_ID_BUTTON_GAME_LEVEL_TWO:
					selectSideSubWindow();
					chooseLevelTwo();
					break;
				case GUI_ID_BUTTON_GAME_LEVEL_THREE:
					chooseLevelThree();
					break;

				case GUI_ID_BUTTON_SAVE_GAME_YES:
					chooseSaveGame();
					if(PlayerQuit)
						PlayerCanQuit = true;
					break;

				case GUI_ID_BUTTON_SAVE_GAME_NO:
					chooseNotSaveGame();
					if(PlayerQuit)
						PlayerCanQuit = true;
					break;

				case GUI_ID_BUTTON_SET_IP:
					webSet();
					break;

				case GUI_ID_QUIT_BUTTON:
					QuitSubWindow();
					
					//PlayerQuit = true;
					//Context.device->closeDevice();
					break;
				case GUI_ID_BUTTON_QUIT_GAME_YES:
					PlayerQuit = true;
					if(LevelOne || LevelTwo ||PlayOnLineGame)
						SaveGameSubWindow();
					else
						PlayerCanQuit = true;
					break;
				case GUI_ID_BUTTON_QUIT_GAME_NO:
					PlayerQuit = false;
					break;
				case GUI_ID_NEW_WINDOW_BUTTON:
					{
						Context.listbox->addItem(L"Window created");
						Context.counter += 30;
						if (Context.counter > 200)
							Context.counter = 0;

						IGUIWindow* window = env->addWindow(
							rect<s32>(100 + Context.counter, 100 + Context.counter,
							300 + Context.counter, 200 + Context.counter),
							false, // modal?
							L"Test window");

						env->addStaticText(L"Please close me",
							rect<s32>(35,35,140,50),
							true, // border?
							false, // wordwrap?
							window);
					}
					return true;

				case GUI_ID_FILE_OPEN_BUTTON:
					Context.listbox->addItem(L"File open");
					env->addFileOpenDialog(L"Please choose a file.");
					return true;

				case GUI_ID_BUTTON_OPEN_MODEL:
					env->addFileOpenDialog(L"Please select a model file to open");
					break;
				case GUI_ID_BUTTON_SHOW_ABOUT:
					showAboutText();
					break;
				case GUI_ID_BUTTON_SHOW_TOOLBOX:
					createToolBox();
					break;
				case GUI_ID_BUTTON_CHAT_SENT:
					SendMessage = true;
					//sentChatText();
					break;
				default:
					return false;
				}
				break;

			default:
				break;
			}
		}

		return false;
	}

	void closeControlHelp()
	{
		Context.device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_CONTROL_HELP_CONTENT)
			->setVisible(false);
		Context.device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_CLOSE_CONTROL_HELP)
			->setVisible(false);
		
	}
	void chooseSaveGame()
	{
		/*Context.device->getGUIEnvironment()->addMessageBox(L"warning",
			L"Player choose to save game",false);*/
		PlayerSaveGame = true;
	}
	void chooseNotSaveGame()
	{
		/*Context.device->getGUIEnvironment()->addMessageBox(L"warning",
			L"Player doesnot choose to save game",false);*/
		PlayerNotSaveGame = true;
	}
	void chooseLevelOne()
	{
		if(LevelTwo)
			return;
		//Context.device->getGUIEnvironment()->addMessageBox(L"warning",L"Player chooses level one game",false);
		LevelOne = true;
	}
	void chooseLevelTwo()
	{
		if(LevelOne)
			return;
		LevelTwo = true;
	}
	void chooseLevelThree()
	{
	}
	bool IsLevelOne()
	{
		//if(LevelOne)
		//Context.device->getGUIEnvironment()->addMessageBox(L"warning",L"Save Unfinished Game?",false);
		return LevelOne;
	}
	bool IsLevelTwo()
	{
		return LevelTwo;
	}
	bool IsPlayerUndo()
	{
		return PlayerUndo;
	}
	bool CanPlayerUndo()
	{
		if(undotimes < 3)
			return true;
		else
			PlayerCannotUndo = true;
		return false;
	}
	bool IsPlayerStartUnfinishedGame()
	{
		return StartUnfinishedGame;
	}

	bool IsPlayerSaveGame()
	{
		return PlayerSaveGame;
	}
	bool IsPlayerNotSaveGame()
	{
		return PlayerNotSaveGame;
	}
	bool IsPlayerCannotUndo()
	{
		return PlayerCannotUndo;
	}
	bool IsStartNewGame()
	{
		return StartNewGame;
	}
	bool IsPlayerEndGame()
	{
		return PlayerEndGame;
	}
	bool IsPlayerSendMessage()
	{
		return SendMessage;
	}
	bool IsPlayerSetWeb()
	{
		return PlayerSetWeb;
	}
	bool IsPlayerStartOnLineGame()
	{
		return PlayOnLineGame;
	}
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	bool IsPlayerPlayBlackSide()
	{
		return BlackSide;
	}
	bool IsPlayerQuit()
	{
		return PlayerQuit;
	}
	bool IsPlayerCanQuit()
	{
		return PlayerCanQuit;
	}
	bool IsPlayBlackSide()
	{
		return PlayBlackSide;
	}
	bool IsPlayWhiteSide()
	{
		return PlayWhiteSide;
	}
	
	//handle "menu item clicked" events
	void OnMenuItemSelected(IGUIContextMenu* menu)
	{
		s32 id = menu->getItemCommandId(menu->getSelectedItem());
		IGUIEnvironment* env = Context.device->getGUIEnvironment();

		switch(id)
		{
		case GUI_ID_CLEAR_RECORD:
			clearGameRecord();
			break;
		case GUI_ID_START_UNFINISHED_GAME_WITH_COMPUTER:
			StartUnfinishedGame = true;
			break;
		case GUI_ID_OPEN_MODEL:
			env->addFileOpenDialog(L"Please select a medel file to open");
			break;
		case GUI_ID_QUIT:
			QuitSubWindow();
			break;
		case GUI_ID_ABOUT:
			showAboutText();
			break;
		case GUI_ID_SHOW_GAME_RECORD:
			showGameRecord();
			break;
		case GUI_ID_START_ONLINE_GAME:
			if(LevelOne || LevelTwo)
				SaveGameSubWindow();
			showWebSetWindow();
			break;
		case GUI_ID_START_NEW_GAME_WITH_COMPUTER:
			if(LevelOne)
				SaveGameSubWindow();
			if(LevelTwo)
				SaveGameSubWindow();
			StartNewGame = true;
			gameLevelSelect();
			break;
		case GUI_ID_SKY_BOX_VISIBLE: // View -> Skybox
			menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
			scene::ISceneNode* SkyBox = Context.device->getSceneManager()->getSceneNodeFromId(NODE_ID_SKYBOX);
			SkyBox->setVisible(!SkyBox->isVisible());
			break;
		
		}
	}

	void showAboutText()
	{
		Context.device->getGUIEnvironment()->addMessageBox(
			Caption.c_str(),MessageText.c_str());
	}
	void clearGameRecord()
	{
		ifstream ifileWin("Winning Record.txt");
		ifstream ifileLose("Losing Record.txt");
		ifstream ifileTie("Tie Record.txt");
		ofstream ofileWin("Winning Record.txt");
		ofstream ofileLose("Losing Record.txt");
		ofstream ofileTie("Tie Record.txt");
		ofileWin<<"";
		ofileLose<<"";
		ofileTie<<"";
	}
	void showGameRecord()
	{
		ifstream ifileWin("Winning Record.txt");
		ifstream ifileLose("Losing Record.txt");
		ifstream ifileTie("Tie Record.txt");
		/*int wintime = 0;
		int losetime = 0;
		int tietime = 0;
		ifileTie>>tietime;
		ifileWin>>wintime;
		ifileLose>>losetime;*/
		std::string winstr;
		std::string losestr;
		std::string tiestr;
		getline(ifileWin,winstr);
		getline(ifileLose,losestr);
		getline(ifileTie,tiestr);
		/*winstr = itoa(wintime);
		losestr = itoa(lostime);
		tiestr = itoa(tietime);*/
		char* winchar = (char*)winstr.c_str();
		char* losechar = (char*)losestr.c_str();
		char* tiechar = (char* )tiestr.c_str();
		wchar_t* winbuffer;
		wchar_t* losebuffer;
		wchar_t* tiebuffer;
		if(winchar)
		{
			size_t nu = strlen(winchar);
			size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)winchar,int(nu),NULL,0);
			winbuffer=0;
			winbuffer = new wchar_t[n+1];
			//if(n>=len) n=len-1;
			MultiByteToWideChar(CP_ACP,0,(const char *)winchar,int(nu),winbuffer,int(n));    
		}
		if(losechar)
		{
			size_t nu = strlen(losechar);
			size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)losechar,int(nu),NULL,0);
			losebuffer=0;
			losebuffer = new wchar_t[n+1];
			//if(n>=len) n=len-1;
			MultiByteToWideChar(CP_ACP,0,(const char *)losechar,int(nu),losebuffer,int(n));    
		}
		if(tiechar)
		{
			size_t nu = strlen(tiechar);
			size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)tiechar,int(nu),NULL,0);
			tiebuffer=0;
			tiebuffer = new wchar_t[n+1];
			//if(n>=len) n=len-1;
			MultiByteToWideChar(CP_ACP,0,(const char *)tiechar,int(nu),tiebuffer,int(n));    
		}
		Context.device->getGUIEnvironment()->addMessageBox(
			L"Winning Times:",winbuffer,false);
		Context.device->getGUIEnvironment()->addMessageBox(
			L"Losing Times:",losebuffer,false);
		Context.device->getGUIEnvironment()->addMessageBox(
			L"Tie Times:",tiebuffer,false);
	}
	void SaveGameSubWindow()
	{
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_SAVE_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(200,90,400,400),
			false,L"Save Game?",0,GUI_ID_SAVE_DIALOG_ROOT_WINDOW);
		IGUITabControl* tab_ip = env->addTabControl(
			core::rect<s32>(2,20,400,290),wnd,true,true);
		//IGUITabControl* tab_image = env->addTabControl(
		//	core::rect<s32>(20,20,400,290),wnd,true,true);
		IGUITab* t1 = tab_ip->addTab(L"Save Game");
		//IGUITab* t2 = tab_image->addTab(L"Image Setting");
		env->addStaticText(L"Save Unfinished GAME?",
			core::rect<s32>(2,20,400,50),true,false,t1);
		env->addButton(rect<s32>(2,90,100,120),t1,GUI_ID_BUTTON_SAVE_GAME_YES,L"Yes");
		env->addButton(rect<s32>(102,90,200,120),t1,GUI_ID_BUTTON_SAVE_GAME_NO,L"No!");

		root->bringToFront(root->getElementFromId(666,true));
	}
	void selectSideSubWindow()
	{
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_SAVE_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(200,90,400,400),
			false,L"Select Side",0,GUI_ID_SAVE_DIALOG_ROOT_WINDOW);
		IGUITabControl* tab_ip = env->addTabControl(
			core::rect<s32>(2,20,400,290),wnd,true,true);
		//IGUITabControl* tab_image = env->addTabControl(
		//	core::rect<s32>(20,20,400,290),wnd,true,true);
		IGUITab* t1 = tab_ip->addTab(L"Select Side");
		//IGUITab* t2 = tab_image->addTab(L"Image Setting");
		env->addStaticText(L"SelectSibe?",
			core::rect<s32>(2,20,400,50),true,false,t1);
		env->addButton(rect<s32>(2,90,100,120),t1,GUI_ID_BUTTON_PLAY_BLACKSIDE,L"Black");
		env->addButton(rect<s32>(102,90,200,120),t1,GUI_ID_BUTTON_PLAY_WHITESIDE,L"White");

		root->bringToFront(root->getElementFromId(666,true));
	}
	void QuitSubWindow()
	{
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_SAVE_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(200,90,400,400),
			false,L"Quit?",0,GUI_ID_SAVE_DIALOG_ROOT_WINDOW);
		IGUITabControl* tab_ip = env->addTabControl(
			core::rect<s32>(2,20,400,290),wnd,true,true);
		//IGUITabControl* tab_image = env->addTabControl(
		//	core::rect<s32>(20,20,400,290),wnd,true,true);
		IGUITab* t1 = tab_ip->addTab(L"Quit Game");
		//IGUITab* t2 = tab_image->addTab(L"Image Setting");
		env->addStaticText(L"Quit GAME?",
			core::rect<s32>(2,20,400,50),true,false,t1);
		env->addButton(rect<s32>(2,90,100,120),t1,GUI_ID_BUTTON_QUIT_GAME_YES,L"Yes");
		env->addButton(rect<s32>(102,90,200,120),t1,GUI_ID_BUTTON_QUIT_GAME_NO,L"No!");

		root->bringToFront(root->getElementFromId(666,true));
	}
	void showWebSetWindow()
	{
		ifstream ifile("ipFile.txt");
		ifstream ifile2("serveripFile.txt");
		ifstream ifile3("selfipFile.txt");
		ofstream ofile("ipFile.txt");
		ofstream ofile2("serveripFile.txt");
		ofstream ofile3("selfipFile.txt");
		ofile<<"";
		ofile2<<"";
		ofile3<<"";
		Context.device->getGUIEnvironment()->addButton(rect<s32>(600+550,580,600+650,100+500), 0, GUI_ID_BUTTON_CHAT_SENT,L"SENT");
		IGUIElement* temp = Context.device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_CHAT_SENT_BOX);
		temp->setVisible(true);
		temp->setEnabled(true);
		temp = Context.device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_CHAT_SENT_BOX);
		temp->setVisible(true);
		Context.device->getGUIEnvironment()->addButton(rect<s32>(600+550,549,600+650,560),0,GUI_ID_BUTTON_ONLINE_GAME_SIDE_BLACK,L"sel B");
		temp = Context.device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_ONLINE_GAME_SIDE_BLACK);
		temp->setVisible(true);
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(200,90,400,420),
			false,L"Web Setting",0,GUI_ID_DIALOG_ROOT_WINDOW);
		IGUITabControl* tab_ip = env->addTabControl(
			core::rect<s32>(2,20,400,310),wnd,true,true);
		IGUITab* t1 = tab_ip->addTab(L"IP Setting");
		env->addStaticText(L"Player 2 IP:",
			core::rect<s32>(2,20,400,50),true,false,t1);
		env->addEditBox(L"",core::rect<s32>(2,60,400,80),true,t1,GUI_ID_IP_INPUT);
		env->addStaticText(L"Player-self IP:",
			core::rect<s32>(2,80,400,110),true,false,t1);
		env->addEditBox(L"",core::rect<s32>(2,120,400,140),true,t1,GUI_IP_SELF_IP_INPUT);
		env->addStaticText(L"Sever IP:",
			core::rect<s32>(2,150,400,180),true,false,t1);
		env->addEditBox(L"",core::rect<s32>(2,190,400,210),true,t1,GUI_IP_SERVER_INPUT);

		env->addButton(rect<s32>(2,220,200,250),t1,GUI_ID_BUTTON_SET_IP,L"Set Web");
		root->bringToFront(root->getElementFromId(666,true));
	}
	void webSet()
	{
		core::stringw ipstr;
		core::stringw serveripstr;
		core::stringw selfipstr;
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		ipstr = root->getElementFromId(GUI_ID_IP_INPUT,true)->getText();
		serveripstr = root->getElementFromId(GUI_IP_SERVER_INPUT,true)->getText();
		selfipstr = root->getElementFromId(GUI_IP_SELF_IP_INPUT,true)->getText();
		if(ipstr.size() == 0)
		{
			env->addMessageBox(L"Warning",L"Empty player2 IP address",true);
			return;
		}
		if(selfipstr.size() == 0)
		{
			env->addMessageBox(L"Warning",L"Empty playerself IP address",true);
			return;
		}
		if(serveripstr.size() == 0)
		{
			env->addMessageBox(L"Warning",L"Empty server IP address",true);
			return;
		}
		env->addMessageBox(L"Warning",L"Setting the web",false);
		//env->addMessageBox(L"the input ip",(wchar_t*)ipstr.c_str(),false);
		int size = 0;
		char* ipadd;
		size = WideCharToMultiByte(CP_ACP,0,ipstr.c_str(),-1,NULL
			,0,NULL,NULL);
		ipadd = (char*)malloc(size + 1);
		WideCharToMultiByte(CP_ACP,0,ipstr.c_str(),-1,ipadd,
			size,NULL,NULL);
		std::string ipstdstr;
		ipstdstr = ipadd;
		ofstream ofile("ipFile.txt");
		ofile<<ipstdstr;
		ofile.close();

		int size2 = 0;
		char* serveripadd;
		size2 = WideCharToMultiByte(CP_ACP,0,serveripstr.c_str(),-1,NULL
			,0,NULL,NULL);
		serveripadd = (char*)malloc(size2 + 1);
		WideCharToMultiByte(CP_ACP,0,serveripstr.c_str(),-1,serveripadd,
			size2,NULL,NULL);
		std::string serveripstdstr;
		serveripstdstr = serveripadd;
		ofstream ofile2("serveripFile.txt");
		ofile2<<serveripstdstr;
		ofile2.close();

		int size3 = 0;
		char* selfipadd;
		size3 = WideCharToMultiByte(CP_ACP,0,selfipstr.c_str(),-1,NULL
			,0,NULL,NULL);
		selfipadd = (char*)malloc(size3 + 1);
		WideCharToMultiByte(CP_ACP,0,selfipstr.c_str(),-1,selfipadd,
			size3,NULL,NULL);	
		std::string selfipstdstr;
		selfipstdstr = selfipadd;
		ofstream ofile3("selfipFile.txt");
		ofile3<<selfipstdstr;
		ofile3.close();
		PlayOnLineGame = true;
		PlayerSetWeb = true;
	}

	void gameLevelSelect()
	{
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,45,800,280),
			false,L"Game Level Select",0,GUI_ID_DIALOG_ROOT_WINDOW);

		IGUITabControl* tab = env->addTabControl(
			core::rect<s32>(2,20,1200-602,480-7),wnd,true,true);

		IGUITab* t1 = tab->addTab(L"Level Select");
		env->addButton(rect<s32>(2,40,200,60),t1,GUI_ID_BUTTON_GAME_LEVEL_ONE,L"LEVEL ONE");
		env->addButton(rect<s32>(2,70,200,90),t1,GUI_ID_BUTTON_GAME_LEVEL_TWO,L"LEVEL TWO");
		//env->addButton(rect<s32>(2,100,200,120),t1,GUI_ID_BUTTON_GAME_LEVEL_THREE,L"LEVEL THREE");
		root->bringToFront(root->getElementFromId(666,true));
	}
	void SetPlayBlackSide(bool newPlayBlackSide)
	{
		PlayBlackSide = newPlayBlackSide;
	}
	void SetPlayerSetWeb(bool newPlayerSetWeb)
	{
		PlayerSetWeb = newPlayerSetWeb;
	}
	void SetLevelOne(bool newLevelOne)
	{
		LevelOne = newLevelOne;
	}
	void SetSendMessage(bool newSendMessage)
	{
		SendMessage = newSendMessage;
	}
	void SetPlayerSaveGame(bool saveGame)
	{
		PlayerSaveGame = saveGame;
	}
	void SetPlayerQuit(bool newPlayerQuit)
	{
		PlayerQuit = newPlayerQuit;
	}
	void SetPlayerCannotUndo(bool newPlayerCannotUndo)
	{
		PlayerCannotUndo = newPlayerCannotUndo;
	}
	void SetStartUnfinishedGame(bool newStartUnfinishedGame)
	{
		StartUnfinishedGame = newStartUnfinishedGame;
	}
	void SetPlayerNotSaveGame(bool newPlayerNotSaveGame)
	{
		PlayerNotSaveGame = newPlayerNotSaveGame;
	}
	void SetPlayerUndo(bool newPlayerUndo)
	{
		PlayerUndo = newPlayerUndo;
	}
	void SetUndoTimes()
	{
		undotimes++;
	}
	void SetPlayerOnlineGame(bool newPlayOnLineGame)
	{
		PlayOnLineGame = newPlayOnLineGame;
	}
	void createToolBox()
	{
		IGUIEnvironment* env = Context.device->getGUIEnvironment();
		IGUIElement* root = env->getRootGUIElement();
		IGUIElement* e = root->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW,true);
		if(e)
			e->remove();

		IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,45,800,480),
			false,L"Toolset",0,GUI_ID_DIALOG_ROOT_WINDOW);

		IGUITabControl* tab = env->addTabControl(
			core::rect<s32>(2,20,800-602,480-7),wnd,true,true);

		IGUITab* t1 = tab->addTab(L"Skin");

		env->addStaticText(L"GUI Transp:",
			core::rect<s32>(10,20,200,55), true, false, t1);
		IGUIScrollBar* scrollbar = env->addScrollBar(true,
			core::rect<s32>(10,55,200,75),t1,GUI_ID_TRANSPARENCY_SCROLL_BAR);
		scrollbar->setMax(255);
		scrollbar->setPos(255);

		env->addStaticText(L"GUI Red:",
			core::rect<s32>(10,85,150,120), true, false, t1);
		IGUIScrollBar* scrollbar_red = env->addScrollBar(true,
			core::rect<s32>(10,125,200,145),t1,GUI_ID_COLOR_SCROLL_BAR_RED);
		scrollbar_red->setMax(255);
		scrollbar_red->setPos(255);

		env->addStaticText(L"GUI Green:",
			core::rect<s32>(10,155,150,190), true, false, t1);
		IGUIScrollBar* scrollbar_green = env->addScrollBar(true,
			core::rect<s32>(10,195,200,215),t1,GUI_ID_COLOR_SCROLL_BAR_GREEN);
		scrollbar_green->setMax(255);
		scrollbar_green->setPos(255);

		env->addStaticText(L"GUI Blue:",
			core::rect<s32>(10,225,150,260), true, false, t1);
		IGUIScrollBar* scrollbar_blue = env->addScrollBar(true,
			core::rect<s32>(10,265,200,285),t1,GUI_ID_COLOR_SCROLL_BAR_BLUE);
		scrollbar_blue->setMax(255);
		scrollbar_blue->setPos(255);

		root->bringToFront(root->getElementFromId(666,true));
	}

	void setMessageText(core::stringw mt){ this->MessageText = mt; }
	void setMessageText2(core::stringw mt){this->MessageText2 = mt;}
	void setCaption(core::stringw c){ this->Caption = c; }
	void setCaption2(core::stringw c){this->Caption2 = c;}

private:
	SAppContext & Context;
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
	core::stringw MessageText;
	core::stringw MessageText2;
	core::stringw Caption;
	core::stringw Caption2;
	//core::stringw ipstr;
	SOCKET ConnectSocket;
	bool PlayerEndGame;
	bool PlayerSaveGame;
	bool PlayerNotSaveGame;
	bool PlayerUndo;
	bool PlayerCannotUndo;
	bool LevelOne;
	bool LevelTwo;
	bool StartUnfinishedGame;
	bool StartNewGame;
	bool SendMessage;
	bool PlayOnLineGame;
	bool BlackSide;
	bool PlayBlackSide;
	bool PlayWhiteSide;
	bool PlayerSetWeb;
	bool PlayerQuit;
	bool PlayerCanQuit;
	int undotimes;
};

#endif